﻿/*
 * Copyright (C) 2012, Jesus Requejo <jesus.requejo@gmail.com> 
 * Licensed under Microsoft Public License (Ms-PL)
 * http://www.microsoft.com/en-us/openness/licenses.aspx#MPL
*/

namespace LoaderServiceManager {

   using System;
   using System.IO;
   using System.Windows.Forms;
   using Infrastructure;

   static class Common {

      public static string GetApplicationPathFromOpenFileDialog(string path) {
         string initialPath;
         initialPath = string.IsNullOrWhiteSpace(path) ? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) : path;
         CreateFolderWorkAroundForSystemAccount();
         OpenFileDialog dialog = new OpenFileDialog();
         dialog.InitialDirectory = initialPath;
         dialog.Filter = FileUtils.GetFormattedValidExtensionFilter();
         dialog.Multiselect = false;
         dialog.RestoreDirectory = true;
         dialog.ShowDialog();

         return dialog.FileName;
      }

      public static string IdProperty {
         get {
            return Infrastructure.StronglyTypedProperty<Infrastructure.ApplicationEntry>.Map(x => x.Id);
         }
      }

      public static string NameProperty {
         get {
            return Infrastructure.StronglyTypedProperty<Infrastructure.ApplicationEntry>.Map(x => x.Name);
         }
      }
      public static string PathProperty {
         get {
            return Infrastructure.StronglyTypedProperty<Infrastructure.ApplicationEntry>.Map(x => x.Path);
         }
      }
      public static string EnabledProperty {
         get {
            return Infrastructure.StronglyTypedProperty<Infrastructure.ApplicationEntry>.Map(x => x.Enabled);
         }
      }
      public static string LaunchAtLogonProperty {
         get {
            return Infrastructure.StronglyTypedProperty<Infrastructure.ApplicationEntry>.Map(x => x.LaunchAtLogon);
         }
      }

      /// <summary>
      /// When this application runs under the SYSTEM account,it will try to open a folder that is not there when 
      /// OpenFileDialog browses the file system. This is a horrible workaround for that, all in the name of a better user experience.
      /// </summary>
      /// <remarks>Well, it's not exactly my fault that someone at Microsoft decided to catch the exception and 
      /// show a message box in the OpenFileDialog class when an expected system place (such as user's Desktop folder) 
      /// does not actually exist instead of simply throwing the exception giving the client of the class 
      /// the opportunity to handle the exception :P
      /// 
      /// See: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c29e6bcc-9dbb-49b5-a09a-e1716326cedb
      /// </remarks>
      private static void CreateFolderWorkAroundForSystemAccount() {
         var systemDir = Environment.GetFolderPath(Environment.SpecialFolder.System);
         var path = Path.Combine(systemDir, @"config\systemprofile\Desktop");

         if (!Directory.Exists(path)) {
            Directory.CreateDirectory(path);
         }
      }
   }
}
